package com.coolweather.app.util;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.Build;
import android.util.Log;

public class HttpUtil {
				public static void sendHttpRequest(final String address,final HttpCallbackListener listener) {
								new Thread(new Runnable() {
												public void run() {
																//HttpURLConnection connection = null;
																try {
																				/**
																				URL url = new URL(address);
																				connection = (HttpURLConnection) url.openConnection();
																				connection.setRequestMethod("GET");
																				connection.setConnectTimeout(8000);
																				connection.setReadTimeout(8000);
																				InputStream in = connection.getInputStream();
																				BufferedReader reader = null;
																				reader = new BufferedReader(new InputStreamReader(in));
																				StringBuilder response = new StringBuilder();
																				String line = null;
																				//response.append(reader.readLine());
																				while ((line = reader.readLine()) != null) {
																								Log.d("url",address);
																								response.append(line);
																				}
																				*/
																				HttpClient httpClient = new DefaultHttpClient();
																				HttpGet httpGet = new HttpGet(address);
																				HttpResponse httpResponse = httpClient.execute(httpGet);
																				String response = null;
																				if (httpResponse.getStatusLine().getStatusCode() == 200) {
																								HttpEntity entity = httpResponse.getEntity();
																								response = EntityUtils.toString(entity,"utf-8");
																				}
																				
																				if (listener != null) {
																								//回调onFinish()方法
																								//listener.onFinish(response.toString());
																								listener.onFinish(response);
																				}
																				
																} catch (Exception e) {
																				if (listener != null) {
																								//回调onError()方法
																								listener.onError(e);
																				}
																} finally {
																				/**
																				if (connection != null) {
																								connection.disconnect();
																				}
																				*/
																}
												}
								}).start();
				}

}
